package com.xuanwei.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.bean.result.BaseWxPayResult;
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult;
import com.xuanwei.core.enums.PayHandleType;
import com.xuanwei.core.service.IXwOrderPayLogService;
import com.xuanwei.dao.entity.XwOrderPayLog;
import com.xuanwei.dao.mapper.XwOrderPayLogMapper;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mjy
 * @since 2019-07-11
 */
@Service
public class XwOrderPayLogServiceImpl extends ServiceImpl<XwOrderPayLogMapper, XwOrderPayLog> implements IXwOrderPayLogService {


    @Override
    public void insert(String orderNo, BaseWxPayResult result, PayHandleType payHandleType) {
        insert(orderNo, null, result, payHandleType);
    }

    @Override
    public void insert(String orderNo, String R, BaseWxPayResult result, PayHandleType payHandleType) {
        XwOrderPayLog orderPayLog = baseMapper.selectOne(new QueryWrapper<XwOrderPayLog>().select("id")
                .eq("order_no", orderNo)
                .eq("type", payHandleType.getCode()));
        if (orderPayLog == null) {
            orderPayLog = new XwOrderPayLog();
            orderPayLog.setType(payHandleType.getCode());
            orderPayLog.setCreateTime(LocalDateTime.now());
            orderPayLog.setErrCodeDes(result.getErrCodeDes());
            orderPayLog.setOrderNo(orderNo);
            orderPayLog.setResultCode(result.getResultCode());
            orderPayLog.setReturnCode(result.getReturnCode());
            orderPayLog.setReturnMsg(result.getReturnMsg());
            orderPayLog.setTypeResult(R);
            save(orderPayLog);
        } else {
            orderPayLog.setCreateTime(LocalDateTime.now());
            orderPayLog.setErrCodeDes(result.getErrCodeDes());
            orderPayLog.setResultCode(result.getResultCode());
            orderPayLog.setReturnCode(result.getReturnCode());
            orderPayLog.setReturnMsg(result.getReturnMsg());
            orderPayLog.setTypeResult(R);
            updateById(orderPayLog);
        }
    }
}
